﻿using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;

namespace Yoozoo.Gameplay.RTS
{
    [BurstCompile]
    public struct AnimSpriteJob : IJob
    {
        public AnimSpriteData ps;
        [ReadOnly]
        public ChildBehaviourNativeData childData;

        public void Execute()
        {
            for (int i = 0; i < ps.count; i++)
            {
                var index = i;
                if (ps.valid[index] && !ps.removes[index])
                {
                    var isShadow = ps.isShadow[index];
                    var childIndex = ps.childIndex[index];
                    if (childIndex >= 0 && childData.valid[childIndex])
                    {
                        var animSpriteData = childData.animSpriteData[childIndex];
                        if (isShadow)
                        {
                            ps.positions[index] = animSpriteData.position + animSpriteData.shadowOffset;
                        }
                        else
                        {
                            ps.positions[index] = animSpriteData.position;
                            ps.rotations[index] = animSpriteData.forward;
                            ps.actions[index] = animSpriteData.animationIndex;
                            ps.restarts[index] = animSpriteData.changeAnimationFlag;
                            ps.speeds[index] = animSpriteData.animationSpeed * animSpriteData.configSpeed;
                        }
                        var alpha = animSpriteData.alpha;
                        ps.alphas[index] = (byte) (alpha * 255);
                        var lastScale = ps.spriteSizes[index];
                        var visible = animSpriteData.visible && alpha > 0;
                        if (visible && lastScale <= 0)
                        {
                            ps.spriteSizes[index] = isShadow
                                ? animSpriteData.shadowOriginScale
                                : animSpriteData.spriteOriginScale;// * ps.createScale;
                        }
                        else if (!visible && lastScale > 0)
                        {
                            ps.spriteSizes[index] = 0;
                        }
                        ps.team[index] = animSpriteData.team;
                    }
                }
            }
           
        }
    }
}
